package com.shenmazong.serveredustudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.serveredustudent.pojo.TbClazz;
import com.shenmazong.serveredustudent.service.TbClazzService;
import com.shenmazong.serveredustudent.mapper.TbClazzMapper;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.ListParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class TbClazzServiceImpl extends ServiceImpl<TbClazzMapper, TbClazz>
    implements TbClazzService{

    @Autowired
    private TbClazzMapper clazzMapper;

    @Override
    public ResultResponse getClazzsByStageId(long stageId) {

        if(stageId==0){
            return ResultResponse.FAILED(-1,"参数异常");
        }

        LambdaQueryWrapper<TbClazz> eq = new QueryWrapper<TbClazz>().lambda().eq(TbClazz::getStageId, stageId);

        List<TbClazz> tbClazzes = clazzMapper.selectList(eq);

        return ResultResponse.SUCCESS(tbClazzes);
    }

    @Override
    public ResultResponse createClazz(TbClazz clazz) {

        int insert = clazzMapper.insert(clazz);

        return ResultResponse.SUCCESS(clazz);
    }

    @Override
    public ResultResponse deleteClazz(Long clazzId) {

        if(clazzId==0){
            return ResultResponse.FAILED(-1,"参数异常");
        }

        LambdaQueryWrapper<TbClazz> last = new QueryWrapper<TbClazz>().lambda().eq(TbClazz::getClazzId, clazzId).last("limit 1");

        clazzMapper.delete(last);

        return ResultResponse.SUCCESS("删除成功");
    }

    @Override
    public ResultResponse updateClazz(TbClazz clazz) {

        if(clazz.getClazzId()==null){
            return ResultResponse.FAILED(-1,"参数有误");
        }

        clazzMapper.updateById(clazz);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse getAllClazz(ListParamVo listParamVo) {

        Page<TbClazz> tbClazzPage = new Page<>(listParamVo.getCurrent(), listParamVo.getSize());

        Page<TbClazz> tbClazzPage1 = clazzMapper.selectPage(tbClazzPage, null);

        return ResultResponse.SUCCESS(tbClazzPage1);

    }
}




